package com.oraro.paygatewaybiz.channel.alipay.serviceImpl;

import com.alipay.api.*;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.oraro.paygatewaybiz.constant.Constants;
import com.oraro.paygatewaybiz.exception.RRException;
import com.oraro.paygatewaybiz.module.entity.ChannelEntity;
import com.oraro.paygatewaybiz.module.entity.OrderEntity;
import org.springframework.beans.factory.annotation.Value;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * @Author: jianghuiyong
 * @Date: 2018/12/7 11:22
 * @Version 1.0
 */
public abstract class AbstractAliBaseService {

    @Value("${notify_url.alipay}")
    private String notifyUrl;

    protected AlipayClient getInstance(String appId, String appPrivateKey, String aliPayPublicKey) {
        return new DefaultAlipayClient(Constants.ALIGATEWAYURL, appId, appPrivateKey, Constants.JSON, Constants.UTF8, aliPayPublicKey, Constants.RSA2);
    }

    protected AlipayTradeAppPayRequest getAppReqParams(OrderEntity orderEntity) throws UnsupportedEncodingException {
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(orderEntity.getBody());
        model.setSubject(orderEntity.getSubject());
        model.setOutTradeNo(orderEntity.getOutTradeNo());
        model.setTimeoutExpress(Constants.TIMEOUTEXPRESS);
        model.setTotalAmount(convertCent2Dollar(orderEntity.getTotalFee()));
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(URLEncoder.encode(orderEntity.getOrderId().toString(), Constants.UTF8));
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        return request;
    }

    protected AlipayTradePagePayRequest getNativeReqParams(OrderEntity orderEntity, String returnUrl) throws UnsupportedEncodingException {
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setBody(orderEntity.getBody());
        model.setSubject(orderEntity.getSubject());
        model.setOutTradeNo(orderEntity.getOutTradeNo());
        model.setTimeoutExpress(Constants.TIMEOUTEXPRESS);  //默认30分钟
        model.setTotalAmount(convertCent2Dollar(orderEntity.getTotalFee()));
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setPassbackParams(URLEncoder.encode(orderEntity.getOrderId().toString(), Constants.UTF8));
        alipayTradePagePayRequest.setBizModel(model);
        alipayTradePagePayRequest.setNotifyUrl(notifyUrl);
        alipayTradePagePayRequest.setReturnUrl(returnUrl);
        return alipayTradePagePayRequest;
    }


    protected boolean aliSignCheck(Map<String, String> map, String alipaypublicKey) throws AlipayApiException {
        boolean flag = AlipaySignature.rsaCheckV1(map, alipaypublicKey, Constants.UTF8, Constants.RSA2);
        return flag;
    }

    /**
     * 金额单位(分转元)
     * @param amount
     * @return
     */
    private static String convertCent2Dollar(Long amount) {
        return BigDecimal.valueOf(amount).divide(new BigDecimal(100)).toString();
    }

    /**
     * 元转分，确保price保留两位有效数字
     * @return
     */
    public static long convertDollar2Cent(double price) {
        DecimalFormat df = new DecimalFormat("#.00");
        price = Double.valueOf(df.format(price));
        long money = (long)(price * 100);
        return money;
    }


    }
